const token=localStorage.getItem("token")
const forbidTable=document.getElementById("forbidTable")
const supportTable=document.getElementById("supportTable")
const is_forbid_specialCkb = document.getElementById("is_forbid_specialCkb")
const is_support_specialCkb=document.getElementById("is_support_specialCkb")
const insertBtn=document.getElementById("insertBtn")

var isForbid =false
var isSupport =false
var forbidList=[]
var supportList=[]

is_forbid_specialCkb.addEventListener("click",async function(){
    if (this.checked) {
        isForbid=true
        forbidTable.style.display="table"
        const data=await requestCourse()
        console.log(data.courses)
        displayForbidTable(data.courses)
    } else {
        isForbid=false
        forbidTable.style.display="none"
        forbidList=[]
    }
})
is_support_specialCkb.addEventListener("click",async function(){
    if (this.checked) {
        isSupport=true
        supportTable.style.display="table"
        const data=await requestCourse()
        console.log(data.courses)
        displaySupportTable(data.courses)
    } else {
        isSupport=false
        supportTable.style.display="none"
        supportList=[]
    }
})
insertBtn.addEventListener("click",async function(){
    requestInsertCard()
})

function displayForbidTable(data){
    const tbody = forbidTable.querySelector("tbody");
    // 清空表格内容，避免重复追加行
    tbody.innerHTML = "";
    data.forEach(function (oneCourse) {
        const newRow = document.createElement("tr");
        //遍历对象属性并添加到表格行

        const newData1 = document.createElement("td");
        newData1.textContent = oneCourse.CourseName;
        newRow.appendChild(newData1);

        const newData2 = document.createElement("td");
        newData2.textContent = oneCourse.Introduction;
        newRow.appendChild(newData2);

        const newData3 = document.createElement("td");
        newData3.textContent = oneCourse.CourseSubject;
        newRow.appendChild(newData3);

        const newData4 = document.createElement("td");
        let type=""
        if (oneCourse.IsVipType){
            type="私教"
        }
        if (oneCourse.IsTeamType){
            type="小班"
        }
        if (oneCourse.IsGroupType){
            type="团课"
        }
        newData4.textContent = type;
        newRow.appendChild(newData4);

        //创建复选框单元
        const checkboxCell = document.createElement("td");
        const checkbox = document.createElement("input");
        checkbox.type = "checkbox";

        // 复选框点击事件
        checkbox.addEventListener("click", function () {
            if (this.checked) {
                // 添加到选中数组
                forbidList.push(oneCourse.CourseId);
            } else {
                // 从选中数组中移除
                const index = forbidList.indexOf(oneCourse.CourseId);
                if (index !== -1) {
                    forbidList.splice(index, 1);
                }
            }
            console.log(forbidList);
        });
        checkboxCell.appendChild(checkbox);
        newRow.appendChild(checkboxCell);
        // 将新行添加到表格
        tbody.appendChild(newRow);
    });
}
function displaySupportTable(data){
    const tbody = supportTable.querySelector("tbody");
    // 清空表格内容，避免重复追加行
    tbody.innerHTML = "";
    data.forEach(function (oneCourse) {
        const newRow = document.createElement("tr");
        //遍历对象属性并添加到表格行

        const newData1 = document.createElement("td");
        newData1.textContent = oneCourse.CourseName;
        newRow.appendChild(newData1);

        const newData2 = document.createElement("td");
        newData2.textContent = oneCourse.Introduction;
        newRow.appendChild(newData2);

        const newData3 = document.createElement("td");
        newData3.textContent = oneCourse.CourseSubject;
        newRow.appendChild(newData3);

        const newData4 = document.createElement("td");
        let type=""
        if (oneCourse.IsVipType){
            type="私教"
        }
        if (oneCourse.IsTeamType){
            type="小班"
        }
        if (oneCourse.IsGroupType){
            type="团课"
        }
        newData4.textContent = type;
        newRow.appendChild(newData4);

        //创建复选框单元
        const checkboxCell = document.createElement("td");
        const checkbox = document.createElement("input");
        checkbox.type = "checkbox";

        // 复选框点击事件
        checkbox.addEventListener("click", function () {
            if (this.checked) {
                // 添加到选中数组
                supportList.push(oneCourse.CourseId);
            } else {
                // 从选中数组中移除
                const index = supportList.indexOf(oneCourse.CourseId);
                if (index !== -1) {
                    supportList.splice(index, 1);
                }
            }
            console.log(supportList);
        });
        checkboxCell.appendChild(checkbox);
        newRow.appendChild(checkboxCell);
        // 将新行添加到表格
        tbody.appendChild(newRow);
    });
}

async function requestCourse(){
    const url=`${config.server}`+"/api/select/all/course"
    const data={
    }
    const response=await fetch(url,{
        method:"POST",
        headers: { 'Content-Type': 'application/json' },
        body:JSON.stringify(data)
    })
    const getdata=await response.json()
    if(response.ok){
        console.log("请求成功",getdata)
        return getdata
    }else{
        console.log("请求失败",getdata)
    }
}

const cardNameInput=document.getElementById("card_name")
const cardIntroductionInput=document.getElementById("card_introduction")
const isSupportGroupCbx=document.getElementById("is_support_group")
const isSupportTeamCbx=document.getElementById("is_support_team")
const isSupportVipCbx=document.getElementById("is_support_vip")
const isLimitTimesCbx=document.getElementById("is_limit_times")
const isLimitDaysCbx=document.getElementById("is_limit_days")

const priceInput=document.getElementById("price")

async function requestInsertCard(){
    const url=`${config.server}`+"/api/admin/insert/card"
    if (cardNameInput.value===""){
        alert("请至少填入会员卡名字")
        return
    }
    const data={
        "token":token,
        "card_name":cardNameInput.value,
        "card_introduction":cardIntroductionInput.value,
        "is_support_group":isSupportGroupCbx.checked,
        "is_support_team":isSupportTeamCbx.checked,
        "is_support_vip":isSupportVipCbx.checked,
        "is_limit_days":isLimitDaysCbx.checked,
        "is_limit_times":isLimitTimesCbx.checked,
        "is_forbid_special":isForbid,
        "is_support_special":isSupport,
        "forbid_course_id":forbidList,
        "support_course_id":supportList,
        "price":parseInt(priceInput.value)
    }
    const response=await fetch(url,{
        method:"POST",
        headers: { 'Content-Type': 'application/json' },
        body:JSON.stringify(data)
    })
    const getdata=await response.json()
    if(response.ok){
        alert("插入成功")
        location.reload()
    }else{
        alert("插入失败")
        console.log("获取数据失败",getdata)
    }
}